/*
 * LinkSet.java
 *
 * Created on July 18, 2007, 11:17 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.atomojo.app.client;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 *
 * @author alex
 */
public class LinkSet extends TreeMap<String,List<Link>>
{
   public LinkSet() {
   }
   
   public void addLinks(Map<String,Link> links) {
      for (Link link : links.values()) {
         if (link.getRelation()==null) {
            continue;
         }
         List<Link> list = get(link.getRelation());
         if (list==null) {
            list = new ArrayList<Link>();
            put(link.getRelation(),list);
         }
         list.add(link);
      }
   }
   
   public void mergeLinks(Map<String,Link> links) {
      for (Link link : links.values()) {
         if (link.getRelation()==null) {
            continue;
         }
         List<Link> list = get(link.getRelation());
         if (list==null) {
            list = new ArrayList<Link>();
            put(link.getRelation(),list);
            list.add(link);
         }
      }
   }
   
   public void replaceLinks(Map<String,Link> links) {
      for (Link link : links.values()) {
         if (link.getRelation()==null) {
            continue;
         }
         List<Link> list = get(link.getRelation());
         if (list==null) {
            list = new ArrayList<Link>();
            put(link.getRelation(),list);
         } else {
            list.clear();
         }
         list.add(link);
      }
   }
   
   public void addLinkSet(Map<String,? extends Collection<Link>> links) {
      for (String rel : links.keySet()) {
         Collection toAdd = links.get(rel);
         if (toAdd.size()==0) {
            continue;
         }
         List<Link> list = get(rel);
         if (list==null) {
            list = new ArrayList<Link>();
            put(rel,list);
         }
         list.addAll(toAdd);
      }
   }
   
   public void mergeLinkSet(Map<String,? extends Collection<Link>> links) {
      for (String rel : links.keySet()) {
         Collection toAdd = links.get(rel);
         if (toAdd.size()==0) {
            continue;
         }
         List<Link> list = get(rel);
         if (list==null) {
            list = new ArrayList<Link>();
            put(rel,list);
            list.addAll(toAdd);
         }
      }
   }
   public void replaceLinkSet(Map<String,? extends Collection<Link>> links) {
      for (String rel : links.keySet()) {
         Collection toAdd = links.get(rel);
         if (toAdd.size()==0) {
            continue;
         }
         List<Link> list = get(rel);
         if (list==null) {
            list = new ArrayList<Link>();
            put(rel,list);
         } else {
            list.clear();
         }
         list.addAll(toAdd);
      }
   }
   public void put(String rel, Link link) {
      List<Link> list = get(rel);
      if (list==null) {
         list = new ArrayList<Link>();
         put(rel,list);
      }
      list.add(link);
   }
   
}
